package com.eminem.controller;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Map;


public class BaseServlet extends HttpServlet {

    protected String getUserId(HttpServletRequest req){
        //获取当前用户登录id
        Map userId = (Map)req.getSession().getAttribute("UserInfo");
        return userId.get("userId").toString();
    }
    protected String getUserName(HttpServletRequest req){
        //获取当前用户登录用户名
        Map userInfo = (Map) req.getSession().getAttribute("UserInfo");
        return userInfo.get("username").toString();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {

        //获取一部分地址栏路径
        String uri = req.getRequestURI();
//        System.out.println(uri);
        //这是切割后得到的方法名
        String uriName = uri.substring(uri.lastIndexOf("/") + 1);
//        System.out.println(uriName);
        Class<? extends BaseServlet> aClass = this.getClass();
        try {
            Method method = aClass.getDeclaredMethod(uriName, HttpServletRequest.class, HttpServletResponse.class);
            //代理去执行对应的方法
            method.invoke(aClass.newInstance(),req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
